/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import Vue from 'vue'
import store from '@/conf/home/store'
import localStore from '@/module/util/localStorage'
import i18n from '@/module/i18n/index.js'
import config from '~/external/config'
import Router from 'vue-router'
import { Message } from 'element-ui'

import Permissions from '@/module/permissions'

const findFirstChildren = (prefix) => {
  let menu = localStorage.getItem('menu')
  if (menu) {
    menu = JSON.parse(menu)
    let obj = menu.find(e => e.permission !== prefix + ':list' && e.permission.indexOf(prefix) === 0 && e.resourceUrl !== '')
    return obj && obj.resourceUrl
  }
  return '/'
}

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'index',
      redirect: {
        name: 'scheduler-overview'
      }
    },
    {
      path: '/home',
      name: 'home',
      // redirect: {
      //   name: 'scheduler-overview'
      // },
      component: resolve => require(['../pages/home/index'], resolve),
      meta: {
        title: `${i18n.$t('Home')}`,
        refreshInSwitchedTab: config.refreshInSwitchedTab
        // permission: ['home:list']
      }
    },
    {
      path: '/projects',
      name: 'projects',
      component: resolve => require(['../pages/projects/index'], resolve),
      meta: {
        title: `${i18n.$t('Project')}`,
        permission: ['projects:list', 'projects:user:list', 'projects:common:list']
      },
      redirect: {
        name: 'projects-list'
      },
      beforeEnter: (to, from, next) => {
        const blacklist = ['projects', 'projects-list']
        const { projectCode } = to.params || {}
        if (!blacklist.includes(to.name) && projectCode && projectCode !== localStore.getItem('projectCode')) {
          store.dispatch('projects/getProjectByCode', projectCode).then(res => {
            store.commit('dag/setProjectId', res.id)
            store.commit('dag/setProjectCode', res.code)
            store.commit('dag/setProjectName', res.name)
            localStore.setItem('projectId', res.id)
            localStore.setItem('projectCode', res.code)
            localStore.setItem('projectName', res.name)
            next()
          }).catch(e => {
            next({ name: 'projects-list' })
          })
        } else {
          next()
        }
      },
      children: [
        {
          path: '/projects/list',
          name: 'projects-list',
          component: resolve => require(['../pages/projects/pages/list/index'], resolve),
          meta: {
            title: `${i18n.$t('Project')}`,
            refreshInSwitchedTab: config.refreshInSwitchedTab,
            permission: ['projects:list', 'projects:user:list', 'projects:common:list']
          }
        },
        {
          path: '/projects/:projectCode/manage',
          name: 'manage',
          component: resolve => require(['../pages/projects/pages/manage/index.vue'], resolve),
          meta: {
            title: '项目管理',
            refreshInSwitchedTab: config.refreshInSwitchedTab,
            permission: ['projects:list', 'projects:user:list', 'projects:common:list']
          },
          redirect: {
            name: 'projects-manage-index'
          },
          children: [
            {
              path: '/projects/:projectCode/manage/index',
              name: 'projects-manage-index',
              component: resolve => require(['../pages/projects/pages/manage/index/index.vue'], resolve),
              meta: {
                title: '项目概览',
                refreshInSwitchedTab: config.refreshInSwitchedTab,
                permission: ['projects:list', 'projects:user:list', 'projects:common:list']
              }
            },
            {
              path: '/projects/:projectCode/manage/parameter',
              name: 'projects-manage-parameter',
              component: resolve => require(['../pages/projects/pages/manage/parameter/index.vue'], resolve),
              meta: {
                title: '项目级别参数',
                refreshInSwitchedTab: config.refreshInSwitchedTab,
                permission: ['projects:list', 'projects:user:list', 'projects:common:list']
              }
            },
            {
              path: '/projects/:projectCode/manage/preferences',
              name: 'projects-manage-preferences',
              component: resolve => require(['../pages/projects/pages/manage/preferences/index.vue'], resolve),
              meta: {
                title: '项目偏好管理',
                refreshInSwitchedTab: config.refreshInSwitchedTab,
                permission: ['projects:list', 'projects:user:list', 'projects:common:list']
              }
            },
          ]
        },
        {
          path: '/projects/:projectCode/workflow',
          name: 'workflow',
          component: resolve => require(['../pages/projects/pages/workflow/index.vue'], resolve),
          meta: {
            title: '工作流管理',
            refreshInSwitchedTab: config.refreshInSwitchedTab,
            permission: ['projects:list', 'projects:user:list', 'projects:common:list']
          },
          redirect: {
            name: 'projects-workflow-index'
          },
          children: [
            {
              path: '/projects/:projectCode/workflow/kinship',
              name: 'projects-workflow-kinship',
              component: resolve => require(['../pages/projects/pages/workflow/kinship/index'], resolve),
              meta: {
                title: `${i18n.$t('Kinship')}`,
                refreshInSwitchedTab: config.refreshInSwitchedTab,
                permission: ['projects:list', 'projects:user:list', 'projects:common:list']
              }
            },
            {
              path: '/projects/:projectCode/workflow/definition',
              name: 'projects-workflow-definition',
              component: resolve => require(['../pages/projects/pages/workflow/definition/index'], resolve),
              meta: {
                title: `${i18n.$t('Process definition')}`,
                refreshInSwitchedTab: config.refreshInSwitchedTab,
                permission: ['projects:list', 'projects:user:list', 'projects:common:list']
              }
            },
          ]
        },
        {
          path: '/projects/:projectCode/definition',
          name: 'definition',
          component: resolve => require(['../pages/projects/pages/definition/index'], resolve),
          meta: {
            title: `${i18n.$t('Process definition')}`,
            refreshInSwitchedTab: config.refreshInSwitchedTab,
            permission: ['projects:list', 'projects:user:list', 'projects:common:list']
          },
          redirect: {
            name: 'projects-definition-details'
          },
          children: [
            {
              path: '/projects/:projectCode/definition/list/:code',
              name: 'projects-definition-details',
              component: resolve => require(['../pages/projects/pages/definition/pages/details/index'], resolve),
              meta: {
                title: `${i18n.$t('Process definition details')}`,
                refreshInSwitchedTab: config.refreshInSwitchedTab,
                permission: ['projects:list', 'projects:user:list', 'projects:common:list']
              }
            },
            {
              path: '/projects/:projectCode/definition/create',
              name: 'definition-create',
              component: resolve => require(['../pages/projects/pages/definition/pages/create/index'], resolve),
              meta: {
                title: `${i18n.$t('Create process definition')}`,
                permission: ['projects:list', 'projects:user:list', 'projects:common:list']
              }
            },
            {
              path: '/projects/:projectCode/definition/tree/:code',
              name: 'definition-tree-view-index',
              component: resolve => require(['../pages/projects/pages/definition/pages/tree/index'], resolve),
              meta: {
                title: `${i18n.$t('TreeView')}`,
                refreshInSwitchedTab: config.refreshInSwitchedTab,
                permission: ['projects:list', 'projects:user:list', 'projects:common:list']
              }
            },
            {
              path: '/projects/:projectCode/definition/list/timing/:code',
              name: 'definition-timing-details',
              component: resolve => require(['../pages/projects/pages/definition/timing/index'], resolve),
              meta: {
                title: `${i18n.$t('Scheduled task list')}`,
                refreshInSwitchedTab: config.refreshInSwitchedTab,
                permission: ['projects:list', 'projects:user:list', 'projects:common:list']
              }
            }
          ]
        },
        {
          path: '/projects/:projectCode/instance',
          name: 'projects-workflow-instance',
          component: resolve => require(['../pages/projects/pages/instance/index'], resolve),
          meta: {
            title: `${i18n.$t('Process Instance')}`
          },
          redirect: {
            name: 'projects-instance-list'
          },
          children: [
            {
              path: '/projects/:projectCode/instance/list',
              name: 'projects-instance-list',
              component: resolve => require(['../pages/projects/pages/instance/pages/list/index'], resolve),
              meta: {
                title: `${i18n.$t('Process Instance')}`,
                refreshInSwitchedTab: config.refreshInSwitchedTab,
                permission: ['projects:list', 'projects:user:list', 'projects:common:list']
              }
            },
            {
              path: '/projects/:projectCode/instance/list/:id',
              name: 'projects-instance-details',
              component: resolve => require(['../pages/projects/pages/instance/pages/details/index'], resolve),
              meta: {
                title: `${i18n.$t('Process instance details')}`,
                refreshInSwitchedTab: config.refreshInSwitchedTab,
                permission: ['projects:list', 'projects:user:list', 'projects:common:list']
              }
            },
            {
              path: '/projects/:projectCode/instance/gantt/:id',
              name: 'instance-gantt-index',
              component: resolve => require(['../pages/projects/pages/instance/pages/gantt/index'], resolve),
              meta: {
                title: `${i18n.$t('Gantt')}`,
                refreshInSwitchedTab: config.refreshInSwitchedTab,
                permission: ['projects:list', 'projects:user:list', 'projects:common:list']
              }
            }
          ]
        },
        {
          path: '/projects/:projectCode/node-manage',
          name: 'node-task',
          component: resolve => require(['../pages/projects/pages/node-manage/index.vue'], resolve),
          meta: {
            title: '节点管理',
            refreshInSwitchedTab: config.refreshInSwitchedTab,
            permission: ['projects:list', 'projects:user:list', 'projects:common:list']
          },
          redirect: {
            name: 'projects-node-task-definition'
          },
          children: [
            {
              path: '/projects/:projectCode/node-manage/definition',
              name: 'projects-node-task-definition',
              component: resolve => require(['../pages/projects/pages/node-manage/taskDefinition'], resolve),
              meta: {
                title: '节点任务定义',
                refreshInSwitchedTab: config.refreshInSwitchedTab,
                permission: ['projects:list', 'projects:user:list', 'projects:common:list']
              }
            },
            {
              path: '/projects/:projectCode/node-manage/task-instance',
              name: 'projects-node-task-instance',
              component: resolve => require(['../pages/projects/pages/node-manage/taskInstance'], resolve),
              meta: {
                title: `${i18n.$t('Task Instance')}`,
                refreshInSwitchedTab: config.refreshInSwitchedTab,
                permission: ['projects:list', 'projects:user:list', 'projects:common:list']
              }

            },
            {
              path: '/projects/:projectCode/node-manage/task-record',
              name: 'projects-node-task-record',
              component: resolve => require(['../pages/projects/pages/node-manage/taskRecord'], resolve),
              meta: {
                title: `${i18n.$t('Task record')}`,
                refreshInSwitchedTab: config.refreshInSwitchedTab,
                permission: ['projects:list', 'projects:user:list', 'projects:common:list']
              }
            },
            {
              path: '/projects/:projectCode/node-manage/history-task-record',
              name: 'projects-node-history-task-record',
              component: resolve => require(['../pages/projects/pages/node-manage/historyTaskRecord'], resolve),
              meta: {
                title: `${i18n.$t('History task record')}`,
                refreshInSwitchedTab: config.refreshInSwitchedTab,
                permission: ['projects:list', 'projects:user:list', 'projects:common:list']
              }

            }
          ]

        },

      ]
    },
    {
      path: '/resource',
      name: 'resource',
      component: resolve => require(['../pages/resource/index'], resolve),
      redirect: {
        // name: 'file'
        path: findFirstChildren('resource')
      },
      meta: {
        title: `${i18n.$t('Resources')}`,
        refreshInSwitchedTab: config.refreshInSwitchedTab,
        permission: ['resource:list']
      },
      children: [
        {
          path: '/resource/file',
          name: 'file',
          component: resolve => require(['../pages/resource/pages/file/pages/list/index'], resolve),
          meta: {
            title: `${i18n.$t('File Manage')}`,
            refreshInSwitchedTab: config.refreshInSwitchedTab,
            permission: ['resource:file:list']
          }
        },
        {
          path: '/resource/file/create',
          name: 'resource-file-create',
          component: resolve => require(['../pages/resource/pages/file/pages/create/index'], resolve),
          meta: {
            title: `${i18n.$t('Create Resource')}`,
            permission: ['resource:file:list']
          }
        },
        {
          path: '/resource/file/createFolder',
          name: 'resource-file-createFolder',
          component: resolve => require(['../pages/resource/pages/file/pages/createFolder/index'], resolve),
          meta: {
            title: `${i18n.$t('Create Resource')}`,
            permission: ['resource:file:list']
          }
        },
        {
          path: '/resource/file/subFileFolder/:id',
          name: 'resource-file-subFileFolder',
          component: resolve => require(['../pages/resource/pages/file/pages/subFileFolder/index'], resolve),
          meta: {
            title: `${i18n.$t('Create Resource')}`,
            refreshInSwitchedTab: config.refreshInSwitchedTab,
            permission: ['resource:file:list']
          }
        },
        {
          path: '/resource/file/subFile/:id',
          name: 'resource-file-subFile',
          component: resolve => require(['../pages/resource/pages/file/pages/subFile/index'], resolve),
          meta: {
            title: `${i18n.$t('Create Resource')}`,
            refreshInSwitchedTab: config.refreshInSwitchedTab,
            permission: ['resource:file:list']
          }
        },
        {
          path: '/resource/file/list/:id',
          name: 'resource-file-details',
          component: resolve => require(['../pages/resource/pages/file/pages/details/index'], resolve),
          meta: {
            title: `${i18n.$t('File Details')}`,
            refreshInSwitchedTab: config.refreshInSwitchedTab,
            permission: ['resource:file:list']
          }
        },
        {
          path: '/resource/file/subdirectory/:id',
          name: 'resource-file-subdirectory',
          component: resolve => require(['../pages/resource/pages/file/pages/subdirectory/index'], resolve),
          meta: {
            title: `${i18n.$t('File Manage')}`,
            refreshInSwitchedTab: config.refreshInSwitchedTab,
            permission: ['resource:file:list']
          }
        },
        {
          path: '/resource/file/edit/:id',
          name: 'resource-file-edit',
          component: resolve => require(['../pages/resource/pages/file/pages/edit/index'], resolve),
          meta: {
            title: `${i18n.$t('File Details')}`,
            permission: ['resource:file:list']
          }
        },
        {
          path: '/resource/udf',
          name: 'udf',
          component: resolve => require(['../pages/resource/pages/udf/index'], resolve),
          meta: {
            title: `${i18n.$t('UDF manage')}`,
            refreshInSwitchedTab: config.refreshInSwitchedTab,
            permission: ['resource:udf:list']
          },
          children: [
            {
              path: '/resource/udf',
              name: 'resource-udf',
              component: resolve => require(['../pages/resource/pages/udf/pages/resource/index'], resolve),
              meta: {
                title: `${i18n.$t('UDF Resources')}`,
                refreshInSwitchedTab: config.refreshInSwitchedTab,
                permission: ['resource:resource:list']
              }
            },
            {
              path: '/resource/udf/subUdfDirectory/:id',
              name: 'resource-udf-subUdfDirectory',
              component: resolve => require(['../pages/resource/pages/udf/pages/subUdfDirectory/index'], resolve),
              meta: {
                title: `${i18n.$t('UDF Resources')}`,
                refreshInSwitchedTab: config.refreshInSwitchedTab,
                permission: ['resource:resource:list']
              }
            },
            {
              path: '/resource/udf/createUdfFolder',
              name: 'resource-udf-createUdfFolder',
              component: resolve => require(['../pages/resource/pages/udf/pages/createUdfFolder/index'], resolve),
              meta: {
                title: `${i18n.$t('Create Resource')}`,
                permission: ['resource:resource:list']
              }
            },
            {
              path: '/resource/udf/subCreateUdfFolder/:id',
              name: 'resource-udf-subCreateUdfFolder',
              component: resolve => require(['../pages/resource/pages/udf/pages/subUdfFolder/index'], resolve),
              meta: {
                title: `${i18n.$t('Create Resource')}`,
                permission: ['resource:resource:list']
              }
            },
            {
              path: '/resource/func',
              name: 'resource-func',
              component: resolve => require(['../pages/resource/pages/udf/pages/function/index'], resolve),
              meta: {
                title: `${i18n.$t('UDF Function')}`,
                permission: ['resource:func:list']
              }
            }
          ]
        },
        {
          path: '/resource/datasource',
          name: 'datasource',
          component: resolve => require(['../pages/resource/pages/datasource/pages/list/index'], resolve),
          meta: {
            title: `${i18n.$t('Datasource')}`,
            refreshInSwitchedTab: config.refreshInSwitchedTab,
            permission: ['resource:datasource:list']
          }
        }
      ]
    },
    // {
    //   path: '/datasource',
    //   name: 'datasource',
    //   component: resolve => require(['../pages/datasource/index'], resolve),
    //   meta: {
    //     title: `${i18n.$t('Datasource')}`
    //   },
    //   redirect: {
    //     name: 'datasource-list'
    //   },
    //   children: [
    //     {
    //       path: '/datasource/list',
    //       name: 'datasource-list',
    //       component: resolve => require(['../pages/datasource/pages/list/index'], resolve),
    //       meta: {
    //         title: `${i18n.$t('Datasource')}`
    //       }
    //     }
    //   ]
    // },
    {
      path: '/security',
      name: 'security',
      component: resolve => require(['../pages/security/index'], resolve),
      meta: {
        title: `${i18n.$t('Security')}`,
        permission: ['config:list']
      },
      redirect: {
        path: findFirstChildren('config')
        // name: 'worker-groups-manage'
      },
      children: [

        {
          path: '/security/worker-groups',
          name: 'worker-groups-manage',
          component: resolve => require(['../pages/security/pages/workerGroups/index'], resolve),
          meta: {
            title: `${i18n.$t('Worker group manage')}`,
            permission: ['config:worker-group:list']
          }
        },
        {
          path: '/security/worker-groups/createWorkerGroup',
          name: 'worker-groups-createWorkerGroup',
          component: resolve => require(['../pages/security/pages/workerGroups/createWorker/index'], resolve),
          meta: {
            title: '创建work组',
            permission: ['config:worker-group:list']
          }
        },
        {
          path: '/security/environments',
          name: 'environment-manage',
          component: resolve => require(['../pages/security/pages/environment/index'], resolve),
          meta: {
            title: `${i18n.$t('Environment manage')}`,
            permission: ['config:environment:list']
          }
        },
        {
          path: '/security/token',
          name: 'token-manage',
          component: resolve => require(['../pages/security/pages/token/index'], resolve),
          meta: {
            title: `${i18n.$t('Token manage')}`,
            permission: ['config:token:list']
          }
        }

      ]
    },
    {
      path: '/system',
      name: 'system',
      component: resolve => require(['../pages/security/system'], resolve),
      meta: {
        title: '系统管理',
        permission: ['system:list']
      },
      redirect: {
        // name: 'tenement-manage'
        path: findFirstChildren('system')
      },
      children: [
        {
          path: '/system/tenant',
          name: 'tenement-manage',
          component: resolve => require(['../pages/security/pages/tenement/index'], resolve),
          meta: {
            title: `${i18n.$t('Tenant Manage')}`,
            permission: ['system:tenant:list']
          }
        },
        {
          path: '/system/users',
          name: 'users-manage',
          component: resolve => require(['../pages/security/pages/users/index'], resolve),
          meta: {
            title: `${i18n.$t('User Manage')}`,
            refreshInSwitchedTab: config.refreshInSwitchedTab,
            permission: ['system:users:list']
          }
        },
        {
          path: '/system/permission',
          name: 'role-permission',
          component: resolve => require(['../pages/security/pages/permission/index.vue'], resolve),
          meta: {
            title: '角色权限',
            permission: ['system:role_permission:list']
            // refreshInSwitchedTab: config.refreshInSwitchedTab
          }
        },
        {
          path: '/system/queue',
          name: 'queue-manage',
          component: resolve => require(['../pages/security/pages/queue/index'], resolve),
          meta: {
            title: `${i18n.$t('Queue manage')}`,
            permission: ['system:queue:list']
          }
        },
        {
          path: '/system/warning-groups',
          name: 'warning-groups-manage',
          component: resolve => require(['../pages/security/pages/warningGroups/index'], resolve),
          meta: {
            title: `${i18n.$t('Warning group manage')}`,
            permission: ['system:warning-group:list']
          }
        },
        {
          path: '/system/warning-instance',
          name: 'warning-instance-manage',
          component: resolve => require(['../pages/security/pages/warningInstance/index'], resolve),
          meta: {
            title: `${i18n.$t('Warning instance manage')}`,
            permission: ['system:warning-instance:list']
          }
        }

      ]
    },
    {
      path: '/equipment',
      name: 'equipment',
      component: resolve => require(['../pages/equipment/index'], resolve),
      meta: {
        title: '算力注册',
        permission: ['equipment:list']
      },
      redirect: {
        // path: findFirstChildren('equipment')
        path: '/equipment/application'
        // name: 'equipment-route'
      },
      children: [
        {
          path: '/equipment/application',
          name: 'equipment-application',
          component: resolve => require(['../pages/equipment/pages/application/index.vue'], resolve),
          meta: {
            title: '机主申请',
            permission: ['equipment:network:list']
          }
        },
        {
          path: '/equipment/application/ownerApplication',
          name: 'application-ownerApplication',
          component: resolve => require(['../pages/equipment/pages/application/ownerApplication/index.vue'], resolve),
          meta: {
            title: '机主申请新增'
          }
        },
        {
          path: '/equipment/application/details',
          name: 'application-details',
          component: resolve => require(['../pages/equipment/pages/application/components/details.vue'], resolve),
          meta: {
            title: '机主申请详情'
          }
        },
        {
          path: '/equipment/route',
          name: 'equipment-route',
          component: resolve => require(['../pages/equipment/pages/route/index'], resolve),
          meta: {
            title: '算力路由',
            permission: ['equipment:route:list']
          }
        },
        {
          path: '/equipment/network',
          name: 'equipment-network',
          component: resolve => require(['../pages/equipment/pages/network/index'], resolve),
          meta: {
            title: '虚拟网络',
            permission: ['equipment:network:list']
          }
        },

      ]
    },
    {
      path: '/operation',
      name: 'operation',
      component: resolve => require(['../pages/operation/index'], resolve),
      meta: {
        title: '运营中心',
        permission: ['operation:list']
        // refreshInSwitchedTab: config.refreshInSwitchedTab
      },
      redirect: {
        // path: findFirstChildren('operation')
        path: '/operation/operationalOverview'
        // name: 'operation-_source'
      },
      children: [
        // {
        //   path: '/operation/_source',
        //   name: 'operation-_source',
        //   component: resolve => require(['../pages/operation/pages/index/_source/index.vue'], resolve),
        //   meta: {
        //     title: '统计概览',
        //     permission: ['operation:overview:list']
        //   }
        // },
        {
          path: '/operation/operationalOverview',
          name: 'operation-operationalOverview',
          component: resolve => require(['../pages/operation/pages/index/operationalOverview/index.vue'], resolve),
          meta: {
            title: '运营概览',
            permission: ['operation:overview:list']
          }
        },
        {
          path: '/operation/orderList',
          name: 'operation-orderList',
          component: resolve => require(['../pages/operation/pages/index/orderList/index.vue'], resolve),
          meta: {
            title: '订单管理',
            permission: ['operation:order:list']
          }
        },
        {
          path: '/operation/orderList/detail',
          name: 'orderList-detail',
          component: resolve => require(['../pages/operation/pages/index/orderList/orderListDetail/index.vue'], resolve),
          meta: {
            title: '订单详情',
            permission: ['operation:order:list']
          }
        },
        {
          path: '/operation/resourceList',
          name: 'operation-resourceList',
          component: resolve => require(['../pages/operation/pages/index/resourceList/index.vue'], resolve),
          meta: {
            title: '资源管理',
            permission: ['operation:product:list']
          }
        },
        {
          path: '/operation/incomeDetail',
          name: 'operation-incomeDetail',
          component: resolve => require(['../pages/operation/pages/index/incomeDetail/index.vue'], resolve),
          meta: {
            title: '收支明细',
            permission: ['operation:bill:list']
          }
        },
        {
          path: '/operation/operationConfiguration',
          name: 'operation-operationConfiguration',
          component: resolve => require(['../pages/operation/pages/index/operationConfiguration/index.vue'], resolve),
          meta: {
            title: '运营配置',
            permission: ['operation:chargingrule:list']
          }
        },
        // {
        //   path: '/operation/cost',
        //   name: 'operation-cost',
        //   component: resolve => require(['../pages/operation/pages/index/cost/index.vue'], resolve),
        //   meta: {
        //     title: '费用管理'
        //   }
        // }
      ]
    },
    {
      path: '/user',
      name: 'user',
      component: resolve => require(['../pages/user/index'], resolve),
      meta: {
        title: `${i18n.$t('User Center')}`
      },
      redirect: {
        name: 'account'
      },
      children: [
        {
          path: '/user/account',
          name: 'account',
          component: resolve => require(['../pages/user/pages/account/index'], resolve),
          meta: {
            title: `${i18n.$t('User Information')}`
          }
        },
        {
          path: '/user/password',
          name: 'password',
          component: resolve => require(['../pages/user/pages/password/index'], resolve),
          meta: {
            title: `${i18n.$t('Edit password')}`
          }
        },
        {
          path: '/user/token',
          name: 'token',
          component: resolve => require(['../pages/user/pages/token/index'], resolve),
          meta: {
            title: `${i18n.$t('Token manage')}`
          }
        }
      ]
    },
    {
      path: '/monitor',
      name: 'monitor',
      component: resolve => require(['../pages/monitor/index'], resolve),
      meta: {
        title: 'monitor',
        permission: ['monitor:list']
      },
      redirect: {
        path: findFirstChildren('monitor')
        // name: 'servers-master'
      },
      children: [
        {
          path: '/monitor/servers/master',
          name: 'servers-master',
          component: resolve => require(['../pages/monitor/pages/servers/master'], resolve),
          meta: {
            title: `${i18n.$t('Service-Master')}`,
            refreshInSwitchedTab: config.refreshInSwitchedTab,
            permission: ['monitor:service:master:list']
          }
        },
        {
          path: '/monitor/servers/worker',
          name: 'servers-worker',
          component: resolve => require(['../pages/monitor/pages/servers/worker'], resolve),
          meta: {
            title: `${i18n.$t('Service-Worker')}`,
            refreshInSwitchedTab: config.refreshInSwitchedTab,
            permission: ['monitor:service:worker:list']
          }
        },
        {
          path: '/monitor/servers/alert',
          name: 'servers-alert',
          component: resolve => require(['../pages/monitor/pages/servers/alert'], resolve),
          meta: {
            title: 'Alert',
            refreshInSwitchedTab: config.refreshInSwitchedTab
            // permission: ['monitor:service:worker:list']
          }
        },
        {
          path: '/monitor/servers/rpcserver',
          name: 'servers-rpcserver',
          component: resolve => require(['../pages/monitor/pages/servers/rpcserver'], resolve),
          meta: {
            title: 'Rpcserver',
            refreshInSwitchedTab: config.refreshInSwitchedTab
          }
        },
        {
          path: '/monitor/servers/apiserver',
          name: 'servers-apiserver',
          component: resolve => require(['../pages/monitor/pages/servers/apiserver'], resolve),
          meta: {
            title: 'Apiserver',
            refreshInSwitchedTab: config.refreshInSwitchedTab
          }
        },
        {
          path: '/monitor/servers/db',
          name: 'servers-db',
          component: resolve => require(['../pages/monitor/pages/servers/db'], resolve),
          meta: {
            title: 'DB',
            refreshInSwitchedTab: config.refreshInSwitchedTab,
            permission: ['monitor:service:DB:list']
          }
        },
        {
          path: '/monitor/servers/statistics',
          name: 'statistics',
          component: resolve => require(['../pages/monitor/pages/servers/statistics'], resolve),
          meta: {
            title: '任务统计',
            refreshInSwitchedTab: config.refreshInSwitchedTab,
            permission: ['monitor:statistic:task:list']
          }
        }
      ]
    },
    {
      path: '/scheduler',
      name: 'scheduler',
      component: resolve => require(['../pages/scheduler/index'], resolve),
      meta: {
        title: '算力调度',
        permission: ['scheduling:list']
      },
      redirect: {
        path: findFirstChildren('scheduling')
        // name: 'scheduler-overview'
      },
      children: [
        {
          path: '/scheduler/resource',
          name: 'scheduler-resource',
          component: resolve => require(['../pages/scheduler/pages/resource/index'], resolve),
          meta: {
            title: '资源列表',
            refreshInSwitchedTab: config.refreshInSwitchedTab,
            permission: ['scheduling:resource:list']
          }
        },
        {
          path: '/scheduler/overview',
          name: 'scheduler-overview',
          component: resolve => require(['../pages/scheduler/pages/overview/index'], resolve),
          meta: {
            title: '调度中心',
            refreshInSwitchedTab: config.refreshInSwitchedTab,
            permission: ['scheduling:center:list']
          }
        },
        {
          path: '/scheduler/task',
          name: 'scheduler-task-create',
          component: resolve => require(['../pages/scheduler/pages/task/create'], resolve),
          meta: {
            title: '运行任务',
            refreshInSwitchedTab: config.refreshInSwitchedTab
          }
        }
      ]
    },
    {
      path: '/cs-resources',
      name: 'cs-resources',
      component: resolve => require(['../pages/cs-resources/index'], resolve),
      meta: {
        title: '超算资源'
      },
      redirect: {
        name: 'cs-resources-index'
      },
      children: [
        {
          path: '/cs-resources/index',
          name: 'cs-resources-index',
          component: resolve => require(['../pages/cs-resources/pages/home/index'], resolve),
          meta: {
            title: '超算资源'
          }
        },
        {
          path: '/cs-resources/detial/:csName?',
          name: 'cs-resources-detial',
          component: resolve => require(['../pages/cs-resources/pages/home/detail'], resolve),
          meta: {
            title: '超算资源'
          }
        }
      ]
    }
  ]
})

const VueRouterPush = Router.prototype.push
Router.prototype.push = function push (to) {
  return VueRouterPush.call(this, to).catch(err => err)
}

router.beforeEach((to, from, next) => {
  let { meta } = to
  console.log('metameta', to)
  if (meta.permission && meta.permission.length) {
    if (Permissions.getAuth(meta.permission)) {
      const $body = $('body')
      $body.find('.tooltip.fade.top.in').remove()
      if (to.meta.title) {
        document.title = `${to.meta.title} - 算力公共服务平台`
      }
      next()
    } else {
      Message.error('无操作权限')
      // next(from)
    }
  } else {
    next()
  }
})

export default router
